// 获取中国省市区
import { regionData } from 'element-china-area-data'

export default {
  methods: {
    // 省
    getProvince () {
      const provinceOption = regionData.map(item => {
        return { 
          label: item.label
        }
      })
      return provinceOption
    },
    // 市
    // province 省
    // flag 直辖市直接获取三级
    getRegion (province, flag = false) {
      if (!province) return []
      let regionOption = []
      const region = regionData.filter(item => item.label === province)[0].children || []
      if (flag && region[0].label === "市辖区") {
        regionOption = region[0].children.map(item => {
          return {
            label: item.label
          }
        })
      } else {
        regionOption = region.map(item => {
          return {
            label: item.label
          }
        })
      }
      return regionOption
    },
    // 区
    // province 省
    // region 市
    getCity (province, region) {
      if (!province || !region) return []
      let cityOption = []
      const regions = regionData.filter(item => item.label === province)[0].children || []
      const city = regions.filter(item => item.label === region)[0].children || []
      cityOption = city.map(item => {
        return {
          label: item.label
        }
      })
      return cityOption
    }
  },
}
